import { defineStore } from 'pinia'
import { ref } from 'vue'
import { loginAPI } from '../services/api.js'

export const useAuthStore = defineStore('auth', () => {
  const isLoggedIn = ref(false)
  const username = ref('')
  const role = ref('user')

  async function login(name, password) {
    // 调用后端接口
    const user = await loginAPI(name, password)
    isLoggedIn.value = true
    username.value = user.username
    role.value = user.role
  }

  function logout() {
    isLoggedIn.value = false
    username.value = ''
    role.value = 'user'
  }

  return { isLoggedIn, username, role, login, logout }
}) 